package day01;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/2/24 21:44
 * 改动版二分查找
 */
public class BinarySearchPlus {
    public static void main(String[] args) {
        int[] arr = { 1, 2, 4, 6, 8, 10 };
        int index = binarySearchPlus(arr, 6);
        System.out.println(index);
    }


    private static int binarySearchPlus(int[] arr, int target) {
        // end始终作为边界, 不会参与比较!!!
        int start = 0, end = arr.length; // 第一处改动
        while (start < end) {  // 第二处改动
            int middle = (start + end) >>> 1;
            if (arr[middle] == target) {
                return middle;
            } else if (arr[middle] < target) {
                start = middle + 1;
            } else {
                end = middle; // 第三处改动
            }
        }
        return -1;
    }
}
